<?php
error_reporting(E_ALL);

echo "Changing Mode...";

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = $_SERVER['SERVER_ADDR'];
$port = 5567;
echo "Going to address $address on port $port<br />";

//http://www.php.net/manual/en/sockets.constants.php
//socket_create ( int $domain , int $type , int $protocol )
//AF_INET is IPv4 Internet based protocols.
//SOCK_DGRAM is connectionless, unreliable messages of a fixed maximum length). 
//SOL_UDP represents the UDP protocol is based on this socket type.
if (($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === false) {
	echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if ((socket_connect($sock, $address, $port)) === false) {
	echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}


/* Send the Mode Change */
/* The Modes are:
* 0 - Standby
* 1 - Test
* 2 - Battle (Autonomous)
* 3 - Remote
*/

$mode = $_GET['mode'];

//Note: You need to use the double quotes to enable the escape character
$bytesWritten = socket_write($sock, $_GET['mode'] . "\0" );

echo "Bytes written: $bytesWritten";

//not necessary
//socket_close ($sock);
?>